package com.google.precinct.client.mapper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapUIOptions;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Size;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.precinct.client.MainShell;

public class MapPrecinctRootPanel extends Composite {

	private static PrecinctMapPageUiBinder uiBinder = GWT
			.create(PrecinctMapPageUiBinder.class);

	private final MainShell ctr;

	private final String precinctNo;

	interface PrecinctMapPageUiBinder extends UiBinder<Widget, MapPrecinctRootPanel> {
	}

	@UiField
	MapWidget maps;

	@UiField
	SimplePanel leftPane;

	private String province;

	private String municipality;

	public MapPrecinctRootPanel(
			MainShell ctr,
			String precinctNo,
			String area) {
		initWidget(uiBinder.createAndBindUi(this));
		this.ctr = ctr;
		this.precinctNo = precinctNo;

		initializeMaps();
		if (precinctNo != null) {
			leftPane.add(new Map1CityListPanelContents(this, maps, precinctNo));
		} else {
			leftPane.add(new UnusedMapPrecinctListPanel(this, maps, area));
		}
	}

	private void initializeMaps() {
		MapUIOptions options = MapUIOptions.newInstance(Size.newInstance(450, 300));
		options.setScaleControl(true);
		maps.setUI(options);
		for(MapType type : MapType.getDefaultMapTypes()) {
			maps.removeMapType(type);
		}
		maps.removeMapType(MapType.getPhysicalMap());
		maps.addMapType(MapType.getMapmakerNormalMap());
		maps.addMapType(MapType.getMapmakerHybridMap());

		maps.setCenter(LatLng.newInstance(11.781, 122.695), 5, MapType.getMapmakerHybridMap());
	}

	public void selectedAddress(String province, String municipality) {
		this.province = province;
		this.municipality = municipality;
		leftPane.clear();
		leftPane.add(new Map3DropPinPane(maps, precinctNo, province, municipality));
	}

	public void backEditAddress() {
		leftPane.clear();
		leftPane.add(new Map1CityListPanelContents(this, maps, precinctNo, province, municipality));
	}

	public void resetFinderInterface() {
		ctr.resetToHomePage();
	}

}
